
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.db.models.base &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-models-base">
            
  <h1>Source code for django.db.models.base</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">chain</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="k">import</span> <span class="n">checks</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">NON_FIELD_ERRORS</span><span class="p">,</span> <span class="n">FieldDoesNotExist</span><span class="p">,</span> <span class="n">FieldError</span><span class="p">,</span> <span class="n">MultipleObjectsReturned</span><span class="p">,</span>
    <span class="n">ObjectDoesNotExist</span><span class="p">,</span> <span class="n">ValidationError</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">DEFAULT_DB_ALIAS</span><span class="p">,</span> <span class="n">DJANGO_VERSION_PICKLE_KEY</span><span class="p">,</span> <span class="n">DatabaseError</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span>
    <span class="n">connections</span><span class="p">,</span> <span class="n">router</span><span class="p">,</span> <span class="n">transaction</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.db.models.constants</span> <span class="k">import</span> <span class="n">LOOKUP_SEP</span>
<span class="kn">from</span> <span class="nn">django.db.models.deletion</span> <span class="k">import</span> <span class="n">CASCADE</span><span class="p">,</span> <span class="n">Collector</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields.related</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">ForeignObjectRel</span><span class="p">,</span> <span class="n">OneToOneField</span><span class="p">,</span> <span class="n">lazy_related_operation</span><span class="p">,</span> <span class="n">resolve_relation</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.db.models.manager</span> <span class="k">import</span> <span class="n">Manager</span>
<span class="kn">from</span> <span class="nn">django.db.models.options</span> <span class="k">import</span> <span class="n">Options</span>
<span class="kn">from</span> <span class="nn">django.db.models.query</span> <span class="k">import</span> <span class="n">Q</span>
<span class="kn">from</span> <span class="nn">django.db.models.signals</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">class_prepared</span><span class="p">,</span> <span class="n">post_init</span><span class="p">,</span> <span class="n">post_save</span><span class="p">,</span> <span class="n">pre_init</span><span class="p">,</span> <span class="n">pre_save</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.db.models.utils</span> <span class="k">import</span> <span class="n">make_model_tuple</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="k">import</span> <span class="n">RemovedInDjango20Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">force_str</span><span class="p">,</span> <span class="n">force_text</span><span class="p">,</span> <span class="n">python_2_unicode_compatible</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">curry</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves</span> <span class="k">import</span> <span class="nb">zip</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="k">import</span> <span class="n">capfirst</span><span class="p">,</span> <span class="n">get_text_list</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span> <span class="nn">django.utils.version</span> <span class="k">import</span> <span class="n">get_version</span>


<span class="nd">@python_2_unicode_compatible</span>
<span class="k">class</span> <span class="nc">Deferred</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;&lt;Deferred field&gt;&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;&lt;Deferred field&gt;&#39;</span><span class="p">)</span>


<span class="n">DEFERRED</span> <span class="o">=</span> <span class="n">Deferred</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">subclass_exception</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">module</span><span class="p">,</span> <span class="n">attached_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create exception subclass. Used by ModelBase below.</span>

<span class="sd">    If &#39;attached_to&#39; is supplied, the exception will be created in a way that</span>
<span class="sd">    allows it to be pickled, assuming the returned exception class will be added</span>
<span class="sd">    as an attribute to the &#39;attached_to&#39; class.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">class_dict</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;__module__&#39;</span><span class="p">:</span> <span class="n">module</span><span class="p">}</span>
    <span class="k">if</span> <span class="n">attached_to</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">def</span> <span class="nf">__reduce__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="c1"># Exceptions are special - they&#39;ve got state that isn&#39;t</span>
            <span class="c1"># in self.__dict__. We assume it is all in self.args.</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">unpickle_inner_exception</span><span class="p">,</span> <span class="p">(</span><span class="n">attached_to</span><span class="p">,</span> <span class="n">name</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">__setstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">args</span> <span class="o">=</span> <span class="n">args</span>

        <span class="n">class_dict</span><span class="p">[</span><span class="s1">&#39;__reduce__&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">__reduce__</span>
        <span class="n">class_dict</span><span class="p">[</span><span class="s1">&#39;__setstate__&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">__setstate__</span>

    <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">class_dict</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ModelBase</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Metaclass for all models.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">super_new</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ModelBase</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="fm">__new__</span>

        <span class="c1"># Also ensure initialization is only performed for subclasses of Model</span>
        <span class="c1"># (excluding Model class itself).</span>
        <span class="n">parents</span> <span class="o">=</span> <span class="p">[</span><span class="n">b</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">bases</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">)]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">parents</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">super_new</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>

        <span class="c1"># Create the class.</span>
        <span class="n">module</span> <span class="o">=</span> <span class="n">attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;__module__&#39;</span><span class="p">)</span>
        <span class="n">new_attrs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;__module__&#39;</span><span class="p">:</span> <span class="n">module</span><span class="p">}</span>
        <span class="n">classcell</span> <span class="o">=</span> <span class="n">attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;__classcell__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">classcell</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">new_attrs</span><span class="p">[</span><span class="s1">&#39;__classcell__&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">classcell</span>
        <span class="n">new_class</span> <span class="o">=</span> <span class="n">super_new</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">new_attrs</span><span class="p">)</span>
        <span class="n">attr_meta</span> <span class="o">=</span> <span class="n">attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;Meta&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">attr_meta</span><span class="p">,</span> <span class="s1">&#39;abstract&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">attr_meta</span><span class="p">:</span>
            <span class="n">meta</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">new_class</span><span class="p">,</span> <span class="s1">&#39;Meta&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">meta</span> <span class="o">=</span> <span class="n">attr_meta</span>
        <span class="n">base_meta</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">new_class</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="n">app_label</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Look for an application configuration to attach the model to.</span>
        <span class="n">app_config</span> <span class="o">=</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_containing_app_config</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="s1">&#39;app_label&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">app_config</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">abstract</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
                        <span class="s2">&quot;Model class </span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2"> doesn&#39;t declare an explicit &quot;</span>
                        <span class="s2">&quot;app_label and isn&#39;t in an application in &quot;</span>
                        <span class="s2">&quot;INSTALLED_APPS.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
                    <span class="p">)</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="n">app_label</span> <span class="o">=</span> <span class="n">app_config</span><span class="o">.</span><span class="n">label</span>

        <span class="n">new_class</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="s1">&#39;_meta&#39;</span><span class="p">,</span> <span class="n">Options</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="n">app_label</span><span class="p">))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">abstract</span><span class="p">:</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span>
                <span class="s1">&#39;DoesNotExist&#39;</span><span class="p">,</span>
                <span class="n">subclass_exception</span><span class="p">(</span>
                    <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;DoesNotExist&#39;</span><span class="p">),</span>
                    <span class="nb">tuple</span><span class="p">(</span>
                        <span class="n">x</span><span class="o">.</span><span class="n">DoesNotExist</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">parents</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">x</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span>
                    <span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">ObjectDoesNotExist</span><span class="p">,),</span>
                    <span class="n">module</span><span class="p">,</span>
                    <span class="n">attached_to</span><span class="o">=</span><span class="n">new_class</span><span class="p">))</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span>
                <span class="s1">&#39;MultipleObjectsReturned&#39;</span><span class="p">,</span>
                <span class="n">subclass_exception</span><span class="p">(</span>
                    <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;MultipleObjectsReturned&#39;</span><span class="p">),</span>
                    <span class="nb">tuple</span><span class="p">(</span>
                        <span class="n">x</span><span class="o">.</span><span class="n">MultipleObjectsReturned</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">parents</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">x</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span>
                    <span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">MultipleObjectsReturned</span><span class="p">,),</span>
                    <span class="n">module</span><span class="p">,</span>
                    <span class="n">attached_to</span><span class="o">=</span><span class="n">new_class</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">base_meta</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">base_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
                <span class="c1"># Non-abstract child classes inherit some attributes from their</span>
                <span class="c1"># non-abstract parent (unless an ABC comes before it in the</span>
                <span class="c1"># method resolution order).</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="s1">&#39;ordering&#39;</span><span class="p">):</span>
                    <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">ordering</span> <span class="o">=</span> <span class="n">base_meta</span><span class="o">.</span><span class="n">ordering</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="s1">&#39;get_latest_by&#39;</span><span class="p">):</span>
                    <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_latest_by</span> <span class="o">=</span> <span class="n">base_meta</span><span class="o">.</span><span class="n">get_latest_by</span>

        <span class="n">is_proxy</span> <span class="o">=</span> <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">proxy</span>

        <span class="c1"># If the model is a proxy, ensure that the base class</span>
        <span class="c1"># hasn&#39;t been swapped out.</span>
        <span class="k">if</span> <span class="n">is_proxy</span> <span class="ow">and</span> <span class="n">base_meta</span> <span class="ow">and</span> <span class="n">base_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> cannot proxy the swapped model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">base_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">))</span>

        <span class="c1"># Add all attributes to the class.</span>
        <span class="k">for</span> <span class="n">obj_name</span><span class="p">,</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">attrs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="n">obj_name</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>

        <span class="c1"># All the fields of any type declared on this model</span>
        <span class="n">new_fields</span> <span class="o">=</span> <span class="n">chain</span><span class="p">(</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">,</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">,</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">private_fields</span>
        <span class="p">)</span>
        <span class="n">field_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">new_fields</span><span class="p">}</span>

        <span class="c1"># Basic setup for proxy models.</span>
        <span class="k">if</span> <span class="n">is_proxy</span><span class="p">:</span>
            <span class="n">base</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="p">[</span><span class="n">kls</span> <span class="k">for</span> <span class="n">kls</span> <span class="ow">in</span> <span class="n">parents</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">kls</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)]:</span>
                <span class="k">if</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                            <span class="s2">&quot;Abstract base class containing model fields not &quot;</span>
                            <span class="s2">&quot;permitted for proxy model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="n">name</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">continue</span>
                <span class="k">if</span> <span class="n">base</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">base</span> <span class="o">=</span> <span class="n">parent</span>
                <span class="k">elif</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Proxy model &#39;</span><span class="si">%s</span><span class="s2">&#39; has more than one non-abstract model base class.&quot;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">base</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Proxy model &#39;</span><span class="si">%s</span><span class="s2">&#39; has no non-abstract model base class.&quot;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">setup_proxy</span><span class="p">(</span><span class="n">base</span><span class="p">)</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span> <span class="o">=</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span> <span class="o">=</span> <span class="n">new_class</span>

        <span class="c1"># Collect the parent links for multi-table inheritance.</span>
        <span class="n">parent_links</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">([</span><span class="n">new_class</span><span class="p">]</span> <span class="o">+</span> <span class="n">parents</span><span class="p">):</span>
            <span class="c1"># Conceptually equivalent to `if base is Model`.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="c1"># Skip concrete parent classes.</span>
            <span class="k">if</span> <span class="n">base</span> <span class="o">!=</span> <span class="n">new_class</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="c1"># Locate OneToOneField instances.</span>
            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">OneToOneField</span><span class="p">):</span>
                    <span class="n">related</span> <span class="o">=</span> <span class="n">resolve_relation</span><span class="p">(</span><span class="n">new_class</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
                    <span class="n">parent_links</span><span class="p">[</span><span class="n">make_model_tuple</span><span class="p">(</span><span class="n">related</span><span class="p">)]</span> <span class="o">=</span> <span class="n">field</span>

        <span class="c1"># Track fields inherited from base models.</span>
        <span class="n">inherited_attributes</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="c1"># Do the appropriate setup for any model parents.</span>
        <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="n">new_class</span><span class="o">.</span><span class="n">mro</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">base</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">parents</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">):</span>
                <span class="c1"># Things without _meta aren&#39;t functional models, so they&#39;re</span>
                <span class="c1"># uninteresting parents.</span>
                <span class="n">inherited_attributes</span> <span class="o">|=</span> <span class="nb">set</span><span class="p">(</span><span class="n">base</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
                <span class="k">continue</span>

            <span class="n">parent_fields</span> <span class="o">=</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span> <span class="o">+</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
                <span class="c1"># Check for clashes between locally declared fields and those</span>
                <span class="c1"># on the base classes.</span>
                <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">parent_fields</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">FieldError</span><span class="p">(</span>
                            <span class="s1">&#39;Local field </span><span class="si">%r</span><span class="s1"> in class </span><span class="si">%r</span><span class="s1"> clashes with field of &#39;</span>
                            <span class="s1">&#39;the same name from base class </span><span class="si">%r</span><span class="s1">.&#39;</span> <span class="o">%</span> <span class="p">(</span>
                                <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                                <span class="n">name</span><span class="p">,</span>
                                <span class="n">base</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                            <span class="p">)</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">inherited_attributes</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

                <span class="c1"># Concrete classes...</span>
                <span class="n">base</span> <span class="o">=</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span>
                <span class="n">base_key</span> <span class="o">=</span> <span class="n">make_model_tuple</span><span class="p">(</span><span class="n">base</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">base_key</span> <span class="ow">in</span> <span class="n">parent_links</span><span class="p">:</span>
                    <span class="n">field</span> <span class="o">=</span> <span class="n">parent_links</span><span class="p">[</span><span class="n">base_key</span><span class="p">]</span>
                <span class="k">elif</span> <span class="ow">not</span> <span class="n">is_proxy</span><span class="p">:</span>
                    <span class="n">attr_name</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_ptr&#39;</span> <span class="o">%</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>
                    <span class="n">field</span> <span class="o">=</span> <span class="n">OneToOneField</span><span class="p">(</span>
                        <span class="n">base</span><span class="p">,</span>
                        <span class="n">on_delete</span><span class="o">=</span><span class="n">CASCADE</span><span class="p">,</span>
                        <span class="n">name</span><span class="o">=</span><span class="n">attr_name</span><span class="p">,</span>
                        <span class="n">auto_created</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                        <span class="n">parent_link</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="p">)</span>

                    <span class="k">if</span> <span class="n">attr_name</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">FieldError</span><span class="p">(</span>
                            <span class="s2">&quot;Auto-generated field &#39;</span><span class="si">%s</span><span class="s2">&#39; in class </span><span class="si">%r</span><span class="s2"> for &quot;</span>
                            <span class="s2">&quot;parent_link to base class </span><span class="si">%r</span><span class="s2"> clashes with &quot;</span>
                            <span class="s2">&quot;declared field of the same name.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                                <span class="n">attr_name</span><span class="p">,</span>
                                <span class="n">name</span><span class="p">,</span>
                                <span class="n">base</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                            <span class="p">)</span>
                        <span class="p">)</span>

                    <span class="c1"># Only add the ptr field if it&#39;s not already present;</span>
                    <span class="c1"># e.g. migrations will already have it specified</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">new_class</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">):</span>
                        <span class="n">new_class</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="n">attr_name</span><span class="p">,</span> <span class="n">field</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">field</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">parents</span><span class="p">[</span><span class="n">base</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">base_parents</span> <span class="o">=</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">parents</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

                <span class="c1"># Add fields from abstract base class if it wasn&#39;t overridden.</span>
                <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">parent_fields</span><span class="p">:</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">field_names</span> <span class="ow">and</span>
                            <span class="n">field</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">new_class</span><span class="o">.</span><span class="vm">__dict__</span> <span class="ow">and</span>
                            <span class="n">field</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">inherited_attributes</span><span class="p">):</span>
                        <span class="n">new_field</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
                        <span class="n">new_class</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">new_field</span><span class="p">)</span>
                        <span class="c1"># Replace parent links defined on this base by the new</span>
                        <span class="c1"># field. It will be appropriately resolved if required.</span>
                        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">one_to_one</span><span class="p">:</span>
                            <span class="k">for</span> <span class="n">parent</span><span class="p">,</span> <span class="n">parent_link</span> <span class="ow">in</span> <span class="n">base_parents</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                                <span class="k">if</span> <span class="n">field</span> <span class="o">==</span> <span class="n">parent_link</span><span class="p">:</span>
                                    <span class="n">base_parents</span><span class="p">[</span><span class="n">parent</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_field</span>

                <span class="c1"># Pass any non-abstract parent classes onto child.</span>
                <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">parents</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">base_parents</span><span class="p">)</span>

            <span class="c1"># Inherit private fields (like GenericForeignKey) from the parent</span>
            <span class="c1"># class</span>
            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">private_fields</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">FieldError</span><span class="p">(</span>
                            <span class="s1">&#39;Local field </span><span class="si">%r</span><span class="s1"> in class </span><span class="si">%r</span><span class="s1"> clashes with field of &#39;</span>
                            <span class="s1">&#39;the same name from base class </span><span class="si">%r</span><span class="s1">.&#39;</span> <span class="o">%</span> <span class="p">(</span>
                                <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                                <span class="n">name</span><span class="p">,</span>
                                <span class="n">base</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                            <span class="p">)</span>
                        <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">new_class</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">field</span><span class="p">))</span>

        <span class="c1"># Copy indexes so that index names are unique when models extend an</span>
        <span class="c1"># abstract model.</span>
        <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span><span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">indexes</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">abstract</span><span class="p">:</span>
            <span class="c1"># Abstract base models can&#39;t be instantiated and don&#39;t appear in</span>
            <span class="c1"># the list of models for an app. We do the final setup for them a</span>
            <span class="c1"># little differently from normal models.</span>
            <span class="n">attr_meta</span><span class="o">.</span><span class="n">abstract</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">Meta</span> <span class="o">=</span> <span class="n">attr_meta</span>
            <span class="k">return</span> <span class="n">new_class</span>

        <span class="n">new_class</span><span class="o">.</span><span class="n">_prepare</span><span class="p">()</span>
        <span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">register_model</span><span class="p">(</span><span class="n">new_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">new_class</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">new_class</span>

    <span class="k">def</span> <span class="nf">add_to_class</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># We should call the contribute_to_class method only if it&#39;s bound</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">inspect</span><span class="o">.</span><span class="n">isclass</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="s1">&#39;contribute_to_class&#39;</span><span class="p">):</span>
            <span class="n">value</span><span class="o">.</span><span class="n">contribute_to_class</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_prepare</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates some methods once self._meta has been populated.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">opts</span><span class="o">.</span><span class="n">_prepare</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">:</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">get_next_in_order</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_get_next_or_previous_in_order</span><span class="p">,</span> <span class="n">is_next</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">get_previous_in_order</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_get_next_or_previous_in_order</span><span class="p">,</span> <span class="n">is_next</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

            <span class="c1"># Defer creating accessors on the foreign class until it has been</span>
            <span class="c1"># created and registered. If remote_field is None, we&#39;re ordering</span>
            <span class="c1"># with respect to a GenericForeignKey and don&#39;t know what the</span>
            <span class="c1"># foreign class is - we&#39;ll add those accessors later in</span>
            <span class="c1"># contribute_to_class().</span>
            <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="o">.</span><span class="n">remote_field</span><span class="p">:</span>
                <span class="n">wrt</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">order_with_respect_to</span>
                <span class="n">remote</span> <span class="o">=</span> <span class="n">wrt</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span>
                <span class="n">lazy_related_operation</span><span class="p">(</span><span class="n">make_foreign_order_accessors</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">remote</span><span class="p">)</span>

        <span class="c1"># Give the class a docstring -- its definition.</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">cls</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">(</span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">))</span>

        <span class="n">get_absolute_url_override</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">ABSOLUTE_URL_OVERRIDES</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">label_lower</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">get_absolute_url_override</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;get_absolute_url&#39;</span><span class="p">,</span> <span class="n">get_absolute_url_override</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">opts</span><span class="o">.</span><span class="n">managers</span> <span class="ow">or</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_requires_legacy_default_manager</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;objects&#39;</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Model </span><span class="si">%s</span><span class="s2"> must specify a custom Manager, because it has a &quot;</span>
                    <span class="s2">&quot;field named &#39;objects&#39;.&quot;</span> <span class="o">%</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span>
                <span class="p">)</span>
            <span class="n">manager</span> <span class="o">=</span> <span class="n">Manager</span><span class="p">()</span>
            <span class="n">manager</span><span class="o">.</span><span class="n">auto_created</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="s1">&#39;objects&#39;</span><span class="p">,</span> <span class="n">manager</span><span class="p">)</span>

        <span class="c1"># Set the name of _meta.indexes. This can&#39;t be done in</span>
        <span class="c1"># Options.contribute_to_class() because fields haven&#39;t been added to</span>
        <span class="c1"># the model at that point.</span>
        <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">indexes</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">index</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
                <span class="n">index</span><span class="o">.</span><span class="n">set_name_with_model</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>

        <span class="n">class_prepared</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">cls</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_requires_legacy_default_manager</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>  <span class="c1"># RemovedInDjango20Warning</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span>

        <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">manager_inheritance_from_future</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">future_default_manager</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">default_manager</span>

        <span class="c1"># Step 1: Locate a manager that would have been promoted</span>
        <span class="c1"># to default manager with the legacy system.</span>
        <span class="k">for</span> <span class="n">manager</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">managers</span><span class="p">:</span>
            <span class="n">originating_model</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">_originating_model</span>
            <span class="k">if</span> <span class="p">(</span><span class="bp">cls</span> <span class="ow">is</span> <span class="n">originating_model</span> <span class="ow">or</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">proxy</span> <span class="ow">or</span>
                    <span class="n">originating_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">):</span>

                <span class="k">if</span> <span class="n">manager</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_default_manager</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">opts</span><span class="o">.</span><span class="n">default_manager_name</span><span class="p">:</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s2">&quot;Managers from concrete parents will soon qualify as default &quot;</span>
                        <span class="s2">&quot;managers if they appear before any other managers in the &quot;</span>
                        <span class="s2">&quot;MRO. As a result, &#39;</span><span class="si">{legacy_default_manager}</span><span class="s2">&#39; declared on &quot;</span>
                        <span class="s2">&quot;&#39;</span><span class="si">{legacy_default_manager_model}</span><span class="s2">&#39; will no longer be the &quot;</span>
                        <span class="s2">&quot;default manager for &#39;</span><span class="si">{model}</span><span class="s2">&#39; in favor of &quot;</span>
                        <span class="s2">&quot;&#39;</span><span class="si">{future_default_manager}</span><span class="s2">&#39; declared on &quot;</span>
                        <span class="s2">&quot;&#39;</span><span class="si">{future_default_manager_model}</span><span class="s2">&#39;. &quot;</span>
                        <span class="s2">&quot;You can redeclare &#39;</span><span class="si">{legacy_default_manager}</span><span class="s2">&#39; on &#39;</span><span class="si">{cls}</span><span class="s2">&#39; &quot;</span>
                        <span class="s2">&quot;to keep things the way they are or you can switch to the new &quot;</span>
                        <span class="s2">&quot;behavior right away by setting &quot;</span>
                        <span class="s2">&quot;`Meta.manager_inheritance_from_future` to `True`.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="bp">cls</span><span class="o">=</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                            <span class="n">model</span><span class="o">=</span><span class="n">opts</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                            <span class="n">legacy_default_manager</span><span class="o">=</span><span class="n">manager</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                            <span class="n">legacy_default_manager_model</span><span class="o">=</span><span class="n">manager</span><span class="o">.</span><span class="n">_originating_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                            <span class="n">future_default_manager</span><span class="o">=</span><span class="n">future_default_manager</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                            <span class="n">future_default_manager_model</span><span class="o">=</span><span class="n">future_default_manager</span><span class="o">.</span><span class="n">_originating_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                        <span class="p">),</span>
                        <span class="n">RemovedInDjango20Warning</span><span class="p">,</span> <span class="mi">2</span>
                    <span class="p">)</span>

                    <span class="n">opts</span><span class="o">.</span><span class="n">default_manager_name</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">name</span>
                    <span class="n">opts</span><span class="o">.</span><span class="n">_expire_cache</span><span class="p">()</span>

                <span class="k">break</span>

        <span class="c1"># Step 2: Since there are managers but none of them qualified as</span>
        <span class="c1"># default managers under the legacy system (meaning that there are</span>
        <span class="c1"># managers from concrete parents that would be promoted under the</span>
        <span class="c1"># new system), we need to create a new Manager instance for the</span>
        <span class="c1"># &#39;objects&#39; attribute as a deprecation shim.</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># If the &quot;future&quot; default manager was auto created there is no</span>
            <span class="c1"># point warning the user since it&#39;s basically the same manager.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">future_default_manager</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;Managers from concrete parents will soon qualify as &quot;</span>
                    <span class="s2">&quot;default managers. As a result, the &#39;objects&#39; manager &quot;</span>
                    <span class="s2">&quot;won&#39;t be created (or recreated) automatically &quot;</span>
                    <span class="s2">&quot;anymore on &#39;</span><span class="si">{model}</span><span class="s2">&#39; and &#39;</span><span class="si">{future_default_manager}</span><span class="s2">&#39; &quot;</span>
                    <span class="s2">&quot;declared on &#39;</span><span class="si">{future_default_manager_model}</span><span class="s2">&#39; will be &quot;</span>
                    <span class="s2">&quot;promoted to default manager. You can declare &quot;</span>
                    <span class="s2">&quot;explicitly `objects = models.Manager()` on &#39;</span><span class="si">{cls}</span><span class="s2">&#39; &quot;</span>
                    <span class="s2">&quot;to keep things the way they are or you can switch &quot;</span>
                    <span class="s2">&quot;to the new behavior right away by setting &quot;</span>
                    <span class="s2">&quot;`Meta.manager_inheritance_from_future` to `True`.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="bp">cls</span><span class="o">=</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                        <span class="n">model</span><span class="o">=</span><span class="n">opts</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                        <span class="n">future_default_manager</span><span class="o">=</span><span class="n">future_default_manager</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                        <span class="n">future_default_manager_model</span><span class="o">=</span><span class="n">future_default_manager</span><span class="o">.</span><span class="n">_originating_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                    <span class="p">),</span>
                    <span class="n">RemovedInDjango20Warning</span><span class="p">,</span> <span class="mi">2</span>
                <span class="p">)</span>

            <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_base_manager</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">base_manager</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_default_manager</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">default_manager</span>


<span class="k">class</span> <span class="nc">ModelState</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A class for storing instance state</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">db</span>
        <span class="c1"># If true, uniqueness validation checks will consider this a new, as-yet-unsaved object.</span>
        <span class="c1"># Necessary for correct validation of new instances of objects with explicit (non-auto) PKs.</span>
        <span class="c1"># This impacts validation only; it has no effect on the actual save.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adding</span> <span class="o">=</span> <span class="kc">True</span>


<div class="viewcode-block" id="Model"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model">[docs]</a><span class="k">class</span> <span class="nc">Model</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">ModelBase</span><span class="p">)):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># Alias some things as locals to avoid repeat global lookups</span>
        <span class="bp">cls</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">_setattr</span> <span class="o">=</span> <span class="nb">setattr</span>
        <span class="n">_DEFERRED</span> <span class="o">=</span> <span class="n">DEFERRED</span>

        <span class="n">pre_init</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># Set up the storage for instance state</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_state</span> <span class="o">=</span> <span class="n">ModelState</span><span class="p">()</span>

        <span class="c1"># There is a rather weird disparity here; if kwargs, it&#39;s set, then args</span>
        <span class="c1"># overrides it. It should be one or the other; don&#39;t duplicate the work</span>
        <span class="c1"># The reason for the kwargs check is that standard iterator passes in by</span>
        <span class="c1"># args, and instantiation for iteration is 33% faster.</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">):</span>
            <span class="c1"># Daft, but matches old exception sans the err msg.</span>
            <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s2">&quot;Number of args exceeds number of fields&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">fields_iter</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">)</span>
            <span class="c1"># The ordering of the zip calls matter - zip throws StopIteration</span>
            <span class="c1"># when an iter throws it. So if the first iter throws it, the second</span>
            <span class="c1"># is *not* consumed. We rely on this, so don&#39;t change the order</span>
            <span class="c1"># without changing the logic.</span>
            <span class="k">for</span> <span class="n">val</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">fields_iter</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">val</span> <span class="ow">is</span> <span class="n">_DEFERRED</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">_setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Slower, kwargs-ready version.</span>
            <span class="n">fields_iter</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">val</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">fields_iter</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">val</span> <span class="ow">is</span> <span class="n">_DEFERRED</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">_setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
                <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="c1"># Now we&#39;re left with the unprocessed fields that *must* come from</span>
        <span class="c1"># keywords, or default.</span>

        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields_iter</span><span class="p">:</span>
            <span class="n">is_related_object</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="c1"># Virtual field</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">column</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="n">ForeignObjectRel</span><span class="p">):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="c1"># Assume object instance was passed in.</span>
                        <span class="n">rel_obj</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                        <span class="n">is_related_object</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="c1"># Object instance wasn&#39;t passed in -- must be an ID.</span>
                            <span class="n">val</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                            <span class="n">val</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="c1"># Object instance was passed in. Special case: You can</span>
                        <span class="c1"># pass in &quot;None&quot; for related objects if it&#39;s allowed.</span>
                        <span class="k">if</span> <span class="n">rel_obj</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
                            <span class="n">val</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">val</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                        <span class="c1"># This is done with an exception rather than the</span>
                        <span class="c1"># default argument on pop because we don&#39;t want</span>
                        <span class="c1"># get_default() to be evaluated, and then not used.</span>
                        <span class="c1"># Refs #12057.</span>
                        <span class="n">val</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">val</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span>

            <span class="k">if</span> <span class="n">is_related_object</span><span class="p">:</span>
                <span class="c1"># If we are passed a related instance, set it using the</span>
                <span class="c1"># field.name instead of field.attname (e.g. &quot;user&quot; instead of</span>
                <span class="c1"># &quot;user_id&quot;) so that the object gets properly cached (and type</span>
                <span class="c1"># checked) by the RelatedObjectDescriptor.</span>
                <span class="k">if</span> <span class="n">rel_obj</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">_DEFERRED</span><span class="p">:</span>
                    <span class="n">_setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">rel_obj</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">val</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">_DEFERRED</span><span class="p">:</span>
                    <span class="n">_setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">property_names</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">_property_names</span>
            <span class="k">for</span> <span class="n">prop</span> <span class="ow">in</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="c1"># Any remaining kwargs must correspond to properties or</span>
                    <span class="c1"># virtual fields.</span>
                    <span class="k">if</span> <span class="n">prop</span> <span class="ow">in</span> <span class="n">property_names</span> <span class="ow">or</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">prop</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">prop</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">_DEFERRED</span><span class="p">:</span>
                            <span class="n">_setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prop</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">prop</span><span class="p">])</span>
                        <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">prop</span><span class="p">]</span>
                <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="n">FieldDoesNotExist</span><span class="p">):</span>
                    <span class="k">pass</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; is an invalid keyword argument for this function&quot;</span> <span class="o">%</span> <span class="nb">list</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="n">post_init</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>

<div class="viewcode-block" id="Model.from_db"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.from_db">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_db</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">db</span><span class="p">,</span> <span class="n">field_names</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">):</span>
            <span class="n">values</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
            <span class="n">values</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">values</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="ow">in</span> <span class="n">field_names</span> <span class="k">else</span> <span class="n">DEFERRED</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">]</span>
        <span class="n">new</span> <span class="o">=</span> <span class="bp">cls</span><span class="p">(</span><span class="o">*</span><span class="n">values</span><span class="p">)</span>
        <span class="n">new</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">new</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">db</span>
        <span class="k">return</span> <span class="n">new</span></div>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">u</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">UnicodeEncodeError</span><span class="p">,</span> <span class="ne">UnicodeDecodeError</span><span class="p">):</span>
            <span class="n">u</span> <span class="o">=</span> <span class="s1">&#39;[Bad Unicode data]&#39;</span>
        <span class="k">return</span> <span class="n">force_str</span><span class="p">(</span><span class="s1">&#39;&lt;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">u</span><span class="p">))</span>

<div class="viewcode-block" id="Model.__str__"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.__str__">[docs]</a>    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;__unicode__&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> object&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span></div>

<div class="viewcode-block" id="Model.__eq__"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.__eq__">[docs]</a>    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Model</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span> <span class="o">!=</span> <span class="n">other</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">my_pk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">my_pk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">other</span>
        <span class="k">return</span> <span class="n">my_pk</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__eq__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>

<div class="viewcode-block" id="Model.__hash__"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.__hash__">[docs]</a>    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Model instances without primary key value are unhashable&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">hash</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">())</span></div>

    <span class="k">def</span> <span class="nf">__reduce__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span>
        <span class="n">data</span><span class="p">[</span><span class="n">DJANGO_VERSION_PICKLE_KEY</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_version</span><span class="p">()</span>
        <span class="n">class_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
        <span class="k">return</span> <span class="n">model_unpickle</span><span class="p">,</span> <span class="p">(</span><span class="n">class_id</span><span class="p">,),</span> <span class="n">data</span>

    <span class="k">def</span> <span class="nf">__setstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">pickled_version</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">DJANGO_VERSION_PICKLE_KEY</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">pickled_version</span><span class="p">:</span>
            <span class="n">current_version</span> <span class="o">=</span> <span class="n">get_version</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">current_version</span> <span class="o">!=</span> <span class="n">pickled_version</span><span class="p">:</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="s2">&quot;Pickled model instance&#39;s Django version </span><span class="si">%s</span><span class="s2"> does not match &quot;</span>
                    <span class="s2">&quot;the current version </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pickled_version</span><span class="p">,</span> <span class="n">current_version</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Pickled model instance&#39;s Django version is not specified.&quot;</span>

        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_pk_val</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">meta</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">meta</span><span class="p">:</span>
            <span class="n">meta</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_pk_val</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

    <span class="n">pk</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_pk_val</span><span class="p">,</span> <span class="n">_set_pk_val</span><span class="p">)</span>

<div class="viewcode-block" id="Model.get_deferred_fields"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.get_deferred_fields">[docs]</a>    <span class="k">def</span> <span class="nf">get_deferred_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a set containing names of deferred fields for this instance.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span>
            <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="Model.refresh_from_db"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.refresh_from_db">[docs]</a>    <span class="k">def</span> <span class="nf">refresh_from_db</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reloads field values from the database.</span>

<span class="sd">        By default, the reloading happens from the database this instance was</span>
<span class="sd">        loaded from, or by the read router if this instance wasn&#39;t loaded from</span>
<span class="sd">        any database. The using parameter will override the default.</span>

<span class="sd">        Fields can be used to specify which fields to reload. The fields</span>
<span class="sd">        should be an iterable of field attnames. If fields is None, then</span>
<span class="sd">        all non-deferred fields are reloaded.</span>

<span class="sd">        When accessing deferred fields of an instance, the deferred loading</span>
<span class="sd">        of the field will call this method.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">LOOKUP_SEP</span> <span class="ow">in</span> <span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s1">&#39;Found &quot;</span><span class="si">%s</span><span class="s1">&quot; in fields argument. Relations and transforms &#39;</span>
                    <span class="s1">&#39;are not allowed in fields.&#39;</span> <span class="o">%</span> <span class="n">LOOKUP_SEP</span><span class="p">)</span>

        <span class="n">db</span> <span class="o">=</span> <span class="n">using</span> <span class="k">if</span> <span class="n">using</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span>
        <span class="n">db_instance_qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">pk</span><span class="p">)</span>

        <span class="c1"># Use provided fields, if not set then reload all non-deferred fields.</span>
        <span class="n">deferred_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_deferred_fields</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span>
            <span class="n">db_instance_qs</span> <span class="o">=</span> <span class="n">db_instance_qs</span><span class="o">.</span><span class="n">only</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">deferred_fields</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span>
                      <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">deferred_fields</span><span class="p">]</span>
            <span class="n">db_instance_qs</span> <span class="o">=</span> <span class="n">db_instance_qs</span><span class="o">.</span><span class="n">only</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span>

        <span class="n">db_instance</span> <span class="o">=</span> <span class="n">db_instance_qs</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
        <span class="n">non_loaded_fields</span> <span class="o">=</span> <span class="n">db_instance</span><span class="o">.</span><span class="n">get_deferred_fields</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span> <span class="ow">in</span> <span class="n">non_loaded_fields</span><span class="p">:</span>
                <span class="c1"># This field wasn&#39;t refreshed - skip ahead.</span>
                <span class="k">continue</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">db_instance</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
            <span class="c1"># Throw away stale foreign key references.</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
                <span class="n">rel_instance</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">())</span>
                <span class="n">local_val</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">db_instance</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                <span class="n">related_val</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">if</span> <span class="n">rel_instance</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">rel_instance</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">local_val</span> <span class="o">!=</span> <span class="n">related_val</span> <span class="ow">or</span> <span class="p">(</span><span class="n">local_val</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">related_val</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">):</span>
                    <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">()]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">db_instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span></div>

    <span class="k">def</span> <span class="nf">serializable_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the value of the field name for this instance. If the field is</span>
<span class="sd">        a foreign key, returns the id value, instead of the object. If there&#39;s</span>
<span class="sd">        no Field object with this name on the model, the model attribute&#39;s</span>
<span class="sd">        value is returned directly.</span>

<span class="sd">        Used to serialize a field&#39;s value (in the serializer, or form output,</span>
<span class="sd">        for example). Normally, you would just access the attribute directly</span>
<span class="sd">        and not use this method.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_name</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>

<div class="viewcode-block" id="Model.save"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">force_update</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
             <span class="n">update_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Saves the current instance. Override this in a subclass if you want to</span>
<span class="sd">        control the saving process.</span>

<span class="sd">        The &#39;force_insert&#39; and &#39;force_update&#39; parameters can be used to insist</span>
<span class="sd">        that the &quot;save&quot; must be an SQL insert or update (or equivalent for</span>
<span class="sd">        non-SQL backends), respectively. Normally, they should not be set.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Ensure that a model instance without a PK hasn&#39;t been assigned to</span>
        <span class="c1"># a ForeignKey or OneToOneField on this model. If the field is</span>
        <span class="c1"># nullable, allowing the save() would result in silent data loss.</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">is_relation</span><span class="p">:</span>
                <span class="c1"># If the related field isn&#39;t cached, then an instance hasn&#39;t</span>
                <span class="c1"># been assigned and there&#39;s no need to worry about this check.</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">())</span>
                <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">obj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                <span class="c1"># A pk may have been assigned manually to a model instance not</span>
                <span class="c1"># saved to the database (or auto-generated in a case like</span>
                <span class="c1"># UUIDField), but we allow the save to proceed and rely on the</span>
                <span class="c1"># database to raise an IntegrityError if applicable. If</span>
                <span class="c1"># constraints aren&#39;t supported by the database, there&#39;s the</span>
                <span class="c1"># unavoidable risk of data corruption.</span>
                <span class="k">if</span> <span class="n">obj</span> <span class="ow">and</span> <span class="n">obj</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="c1"># Remove the object from a related instance cache.</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">multiple</span><span class="p">:</span>
                        <span class="nb">delattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">())</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;save() prohibited to prevent data loss due to &quot;</span>
                        <span class="s2">&quot;unsaved related object &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span>
                    <span class="p">)</span>

        <span class="n">using</span> <span class="o">=</span> <span class="n">using</span> <span class="ow">or</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">force_insert</span> <span class="ow">and</span> <span class="p">(</span><span class="n">force_update</span> <span class="ow">or</span> <span class="n">update_fields</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot force both insert and updating in model saving.&quot;</span><span class="p">)</span>

        <span class="n">deferred_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_deferred_fields</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">update_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># If update_fields is empty, skip the save. We do also check for</span>
            <span class="c1"># no-op saves later on for inheritance cases. This bailout is</span>
            <span class="c1"># still needed for skipping signal sending.</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">update_fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span>

            <span class="n">update_fields</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">(</span><span class="n">update_fields</span><span class="p">)</span>
            <span class="n">field_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>

            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
                    <span class="n">field_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

                    <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">:</span>
                        <span class="n">field_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>

            <span class="n">non_model_fields</span> <span class="o">=</span> <span class="n">update_fields</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">field_names</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">non_model_fields</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The following fields do not exist in this &quot;</span>
                                 <span class="s2">&quot;model or are m2m fields: </span><span class="si">%s</span><span class="s2">&quot;</span>
                                 <span class="o">%</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">non_model_fields</span><span class="p">))</span>

        <span class="c1"># If saving to the same database, and this model is deferred, then</span>
        <span class="c1"># automatically do a &quot;update_fields&quot; save on the loaded fields.</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">force_insert</span> <span class="ow">and</span> <span class="n">deferred_fields</span> <span class="ow">and</span> <span class="n">using</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span><span class="p">:</span>
            <span class="n">field_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;through&#39;</span><span class="p">):</span>
                    <span class="n">field_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
            <span class="n">loaded_fields</span> <span class="o">=</span> <span class="n">field_names</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">deferred_fields</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">loaded_fields</span><span class="p">:</span>
                <span class="n">update_fields</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">(</span><span class="n">loaded_fields</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">save_base</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="n">force_insert</span><span class="p">,</span>
                       <span class="n">force_update</span><span class="o">=</span><span class="n">force_update</span><span class="p">,</span> <span class="n">update_fields</span><span class="o">=</span><span class="n">update_fields</span><span class="p">)</span></div>
    <span class="n">save</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">save_base</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">raw</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                  <span class="n">force_update</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">update_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Handles the parts of saving which should be done only once per save,</span>
<span class="sd">        yet need to be done in raw saves, too. This includes some sanity</span>
<span class="sd">        checks and signal sending.</span>

<span class="sd">        The &#39;raw&#39; argument is telling save_base not to save any parent</span>
<span class="sd">        models and not to do any changes to the values before save. This</span>
<span class="sd">        is used by fixture loading.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">using</span> <span class="o">=</span> <span class="n">using</span> <span class="ow">or</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">assert</span> <span class="ow">not</span> <span class="p">(</span><span class="n">force_insert</span> <span class="ow">and</span> <span class="p">(</span><span class="n">force_update</span> <span class="ow">or</span> <span class="n">update_fields</span><span class="p">))</span>
        <span class="k">assert</span> <span class="n">update_fields</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">update_fields</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>
        <span class="bp">cls</span> <span class="o">=</span> <span class="n">origin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span>
        <span class="c1"># Skip proxies, but keep the origin as the proxy model.</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">proxy</span><span class="p">:</span>
            <span class="bp">cls</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span>
        <span class="n">meta</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="n">pre_save</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
                <span class="n">sender</span><span class="o">=</span><span class="n">origin</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">raw</span><span class="o">=</span><span class="n">raw</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span>
                <span class="n">update_fields</span><span class="o">=</span><span class="n">update_fields</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">raw</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_save_parents</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">update_fields</span><span class="p">)</span>
            <span class="n">updated</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_save_table</span><span class="p">(</span><span class="n">raw</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">force_insert</span><span class="p">,</span> <span class="n">force_update</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">update_fields</span><span class="p">)</span>
        <span class="c1"># Store the database on which the object was saved</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">using</span>
        <span class="c1"># Once saved, this is no longer a to-be-added instance.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="c1"># Signal that the save is complete</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="n">post_save</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
                <span class="n">sender</span><span class="o">=</span><span class="n">origin</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">created</span><span class="o">=</span><span class="p">(</span><span class="ow">not</span> <span class="n">updated</span><span class="p">),</span>
                <span class="n">update_fields</span><span class="o">=</span><span class="n">update_fields</span><span class="p">,</span> <span class="n">raw</span><span class="o">=</span><span class="n">raw</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span>
            <span class="p">)</span>

    <span class="n">save_base</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_save_parents</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">update_fields</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Saves all the parents of cls using values from self.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">meta</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">for</span> <span class="n">parent</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">meta</span><span class="o">.</span><span class="n">parents</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># Make sure the link fields are synced between parent and self.</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">field</span> <span class="ow">and</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span>
                    <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_save_parents</span><span class="p">(</span><span class="bp">cls</span><span class="o">=</span><span class="n">parent</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span> <span class="n">update_fields</span><span class="o">=</span><span class="n">update_fields</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_save_table</span><span class="p">(</span><span class="bp">cls</span><span class="o">=</span><span class="n">parent</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span> <span class="n">update_fields</span><span class="o">=</span><span class="n">update_fields</span><span class="p">)</span>
            <span class="c1"># Set the parent&#39;s PK value to self.</span>
            <span class="k">if</span> <span class="n">field</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">(</span><span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="p">))</span>
                <span class="c1"># Since we didn&#39;t have an instance of the parent handy set</span>
                <span class="c1"># attname directly, bypassing the descriptor. Invalidate</span>
                <span class="c1"># the related object cache, in case it&#39;s been accidentally</span>
                <span class="c1"># populated. A fresh instance will be re-built from the</span>
                <span class="c1"># database if necessary.</span>
                <span class="n">cache_name</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">()</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_name</span><span class="p">):</span>
                    <span class="nb">delattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_save_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">raw</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                    <span class="n">force_update</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">update_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Does the heavy-lifting involved in saving. Updates or inserts the data</span>
<span class="sd">        for a single table.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">meta</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">non_pks</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">meta</span><span class="o">.</span><span class="n">local_concrete_fields</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">primary_key</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">update_fields</span><span class="p">:</span>
            <span class="n">non_pks</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">non_pks</span>
                       <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">update_fields</span> <span class="ow">or</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="ow">in</span> <span class="n">update_fields</span><span class="p">]</span>

        <span class="n">pk_val</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">(</span><span class="n">meta</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">pk_val</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">pk_val</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">get_pk_value_on_save</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">pk_val</span><span class="p">)</span>
        <span class="n">pk_set</span> <span class="o">=</span> <span class="n">pk_val</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">pk_set</span> <span class="ow">and</span> <span class="p">(</span><span class="n">force_update</span> <span class="ow">or</span> <span class="n">update_fields</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot force an update in save() with no primary key.&quot;</span><span class="p">)</span>
        <span class="n">updated</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="c1"># If possible, try an UPDATE. If that doesn&#39;t update anything, do an INSERT.</span>
        <span class="k">if</span> <span class="n">pk_set</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">force_insert</span><span class="p">:</span>
            <span class="n">base_qs</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">using</span><span class="p">)</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[(</span><span class="n">f</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="k">if</span> <span class="n">raw</span> <span class="k">else</span> <span class="n">f</span><span class="o">.</span><span class="n">pre_save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="kc">False</span><span class="p">)))</span>
                      <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">non_pks</span><span class="p">]</span>
            <span class="n">forced_update</span> <span class="o">=</span> <span class="n">update_fields</span> <span class="ow">or</span> <span class="n">force_update</span>
            <span class="n">updated</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_do_update</span><span class="p">(</span><span class="n">base_qs</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">pk_val</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">update_fields</span><span class="p">,</span>
                                      <span class="n">forced_update</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">force_update</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">updated</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">DatabaseError</span><span class="p">(</span><span class="s2">&quot;Forced update did not affect any rows.&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">update_fields</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">updated</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">DatabaseError</span><span class="p">(</span><span class="s2">&quot;Save with update_fields did not affect any rows.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">updated</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">meta</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">:</span>
                <span class="c1"># If this is a model with an order_with_respect_to</span>
                <span class="c1"># autopopulate the _order field</span>
                <span class="n">field</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">order_with_respect_to</span>
                <span class="n">filter_args</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_filter_kwargs_for_object</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
                <span class="n">order_value</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">filter_args</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_order</span> <span class="o">=</span> <span class="n">order_value</span>

            <span class="n">fields</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">local_concrete_fields</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">pk_set</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="n">f</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">meta</span><span class="o">.</span><span class="n">auto_field</span><span class="p">]</span>

            <span class="n">update_pk</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">auto_field</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">pk_set</span>
            <span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_do_insert</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_base_manager</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">update_pk</span><span class="p">,</span> <span class="n">raw</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">update_pk</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">updated</span>

    <span class="k">def</span> <span class="nf">_do_update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">base_qs</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">pk_val</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">update_fields</span><span class="p">,</span> <span class="n">forced_update</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method will try to update the model. If the model was updated (in</span>
<span class="sd">        the sense that an update query was done and a matching row was found</span>
<span class="sd">        from the DB) the method will return True.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">filtered</span> <span class="o">=</span> <span class="n">base_qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">pk_val</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">values</span><span class="p">:</span>
            <span class="c1"># We can end up here when saving a model in inheritance chain where</span>
            <span class="c1"># update_fields doesn&#39;t target any field in current model. In that</span>
            <span class="c1"># case we just say the update succeeded. Another case ending up here</span>
            <span class="c1"># is a model with just PK - in that case check that the PK still</span>
            <span class="c1"># exists.</span>
            <span class="k">return</span> <span class="n">update_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">filtered</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">select_on_save</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">forced_update</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">filtered</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="c1"># It may happen that the object is deleted from the DB right after</span>
                <span class="c1"># this check, causing the subsequent UPDATE to return zero matching</span>
                <span class="c1"># rows. The same result can occur in some rare cases when the</span>
                <span class="c1"># database returns zero despite the UPDATE being executed</span>
                <span class="c1"># successfully (a row is matched and updated). In order to</span>
                <span class="c1"># distinguish these two cases, the object&#39;s existence in the</span>
                <span class="c1"># database is again checked for if the UPDATE query returns 0.</span>
                <span class="k">return</span> <span class="n">filtered</span><span class="o">.</span><span class="n">_update</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">filtered</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">filtered</span><span class="o">.</span><span class="n">_update</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">_do_insert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">manager</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">update_pk</span><span class="p">,</span> <span class="n">raw</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do an INSERT. If update_pk is defined then this method should return</span>
<span class="sd">        the new pk for the model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">manager</span><span class="o">.</span><span class="n">_insert</span><span class="p">([</span><span class="bp">self</span><span class="p">],</span> <span class="n">fields</span><span class="o">=</span><span class="n">fields</span><span class="p">,</span> <span class="n">return_id</span><span class="o">=</span><span class="n">update_pk</span><span class="p">,</span>
                               <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span> <span class="n">raw</span><span class="o">=</span><span class="n">raw</span><span class="p">)</span>

<div class="viewcode-block" id="Model.delete"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.delete">[docs]</a>    <span class="k">def</span> <span class="nf">delete</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">keep_parents</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">using</span> <span class="o">=</span> <span class="n">using</span> <span class="ow">or</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="p">(</span>
            <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> object can&#39;t be deleted because its </span><span class="si">%s</span><span class="s2"> attribute is set to None.&quot;</span> <span class="o">%</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
        <span class="p">)</span>

        <span class="n">collector</span> <span class="o">=</span> <span class="n">Collector</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>
        <span class="n">collector</span><span class="o">.</span><span class="n">collect</span><span class="p">([</span><span class="bp">self</span><span class="p">],</span> <span class="n">keep_parents</span><span class="o">=</span><span class="n">keep_parents</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">collector</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span></div>

    <span class="n">delete</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_get_FIELD_display</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">flatchoices</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">value</span><span class="p">),</span> <span class="n">strings_only</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_next_or_previous_by_FIELD</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">is_next</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;get_next/get_previous cannot be used on unsaved objects.&quot;</span><span class="p">)</span>
        <span class="n">op</span> <span class="o">=</span> <span class="s1">&#39;gt&#39;</span> <span class="k">if</span> <span class="n">is_next</span> <span class="k">else</span> <span class="s1">&#39;lt&#39;</span>
        <span class="n">order</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span> <span class="k">if</span> <span class="n">is_next</span> <span class="k">else</span> <span class="s1">&#39;-&#39;</span>
        <span class="n">param</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">__</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">op</span><span class="p">):</span> <span class="n">param</span><span class="p">})</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">q</span> <span class="o">|</span> <span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">param</span><span class="p">,</span> <span class="s1">&#39;pk__</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">op</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span><span class="p">})</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">q</span><span class="p">)</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span>
            <span class="s1">&#39;</span><span class="si">%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">order</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">),</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">pk&#39;</span> <span class="o">%</span> <span class="n">order</span>
        <span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">qs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="bp">self</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> matching query does not exist.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_next_or_previous_in_order</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">is_next</span><span class="p">):</span>
        <span class="n">cachename</span> <span class="o">=</span> <span class="s2">&quot;__</span><span class="si">%s</span><span class="s2">_order_cache&quot;</span> <span class="o">%</span> <span class="n">is_next</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cachename</span><span class="p">):</span>
            <span class="n">op</span> <span class="o">=</span> <span class="s1">&#39;gt&#39;</span> <span class="k">if</span> <span class="n">is_next</span> <span class="k">else</span> <span class="s1">&#39;lt&#39;</span>
            <span class="n">order</span> <span class="o">=</span> <span class="s1">&#39;_order&#39;</span> <span class="k">if</span> <span class="n">is_next</span> <span class="k">else</span> <span class="s1">&#39;-_order&#39;</span>
            <span class="n">order_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span>
            <span class="n">filter_args</span> <span class="o">=</span> <span class="n">order_field</span><span class="o">.</span><span class="n">get_filter_kwargs_for_object</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">filter_args</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span>
                <span class="s1">&#39;_order__</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">op</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;_order&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span>
                <span class="p">})</span>
            <span class="p">})</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="n">order</span><span class="p">)[:</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cachename</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cachename</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">prepare_database_save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsaved model instance </span><span class="si">%r</span><span class="s2"> cannot be used in an ORM query.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">get_related_field</span><span class="p">()</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>

<div class="viewcode-block" id="Model.clean"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.clean">[docs]</a>    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for doing any extra model-wide validation after clean() has been</span>
<span class="sd">        called on every field by self.clean_fields. Any ValidationError raised</span>
<span class="sd">        by this method will not be associated with a particular field; it will</span>
<span class="sd">        have a special-case association with the field defined by NON_FIELD_ERRORS.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="Model.validate_unique"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.validate_unique">[docs]</a>    <span class="k">def</span> <span class="nf">validate_unique</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Checks unique constraints on the model and raises ``ValidationError``</span>
<span class="sd">        if any failed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">unique_checks</span><span class="p">,</span> <span class="n">date_checks</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_unique_checks</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">)</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_perform_unique_checks</span><span class="p">(</span><span class="n">unique_checks</span><span class="p">)</span>
        <span class="n">date_errors</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_perform_date_checks</span><span class="p">(</span><span class="n">date_checks</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">date_errors</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_get_unique_checks</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gather a list of checks to perform. Since validate_unique could be</span>
<span class="sd">        called from a ModelForm, some fields may have been excluded; we can&#39;t</span>
<span class="sd">        perform a unique check on a model that is missing fields involved</span>
<span class="sd">        in that check.</span>
<span class="sd">        Fields that did not validate should also be excluded, but they need</span>
<span class="sd">        to be passed in via the exclude argument.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">exclude</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">unique_checks</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">unique_togethers</span> <span class="o">=</span> <span class="p">[(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">)]</span>
        <span class="k">for</span> <span class="n">parent_class</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">parent_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">:</span>
                <span class="n">unique_togethers</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">parent_class</span><span class="p">,</span> <span class="n">parent_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">model_class</span><span class="p">,</span> <span class="n">unique_together</span> <span class="ow">in</span> <span class="n">unique_togethers</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">check</span> <span class="ow">in</span> <span class="n">unique_together</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">check</span><span class="p">:</span>
                    <span class="c1"># If this is an excluded field, don&#39;t add this check.</span>
                    <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                        <span class="k">break</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">unique_checks</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">model_class</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">check</span><span class="p">)))</span>

        <span class="c1"># These are checks for the unique_for_&lt;date/year/month&gt;.</span>
        <span class="n">date_checks</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Gather a list of checks for fields declared as unique and add them to</span>
        <span class="c1"># the list of checks.</span>

        <span class="n">fields_with_class</span> <span class="o">=</span> <span class="p">[(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">)]</span>
        <span class="k">for</span> <span class="n">parent_class</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">():</span>
            <span class="n">fields_with_class</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">parent_class</span><span class="p">,</span> <span class="n">parent_class</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">model_class</span><span class="p">,</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">fields_with_class</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
                <span class="n">name</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span>
                <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
                    <span class="n">unique_checks</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">model_class</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,)))</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_date</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_date</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                    <span class="n">date_checks</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">model_class</span><span class="p">,</span> <span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_date</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_year</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_year</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                    <span class="n">date_checks</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">model_class</span><span class="p">,</span> <span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_year</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_month</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_month</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                    <span class="n">date_checks</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">model_class</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">unique_for_month</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">unique_checks</span><span class="p">,</span> <span class="n">date_checks</span>

    <span class="k">def</span> <span class="nf">_perform_unique_checks</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unique_checks</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">model_class</span><span class="p">,</span> <span class="n">unique_check</span> <span class="ow">in</span> <span class="n">unique_checks</span><span class="p">:</span>
            <span class="c1"># Try to look up an existing object with the same values as this</span>
            <span class="c1"># object&#39;s values for all the unique field.</span>

            <span class="n">lookup_kwargs</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">field_name</span> <span class="ow">in</span> <span class="n">unique_check</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
                <span class="n">lookup_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                <span class="c1"># TODO: Handle multiple backends with different feature flags.</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">lookup_value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span>
                        <span class="p">(</span><span class="n">lookup_value</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span> <span class="ow">and</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span><span class="p">)):</span>
                    <span class="c1"># no value, skip the lookup</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span><span class="p">:</span>
                    <span class="c1"># no need to check for unique primary key when editing</span>
                    <span class="k">continue</span>
                <span class="n">lookup_kwargs</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">field_name</span><span class="p">)]</span> <span class="o">=</span> <span class="n">lookup_value</span>

            <span class="c1"># some fields were skipped, no reason to do the check</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">unique_check</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lookup_kwargs</span><span class="p">):</span>
                <span class="k">continue</span>

            <span class="n">qs</span> <span class="o">=</span> <span class="n">model_class</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">lookup_kwargs</span><span class="p">)</span>

            <span class="c1"># Exclude the current object from the query if we are editing an</span>
            <span class="c1"># instance (as opposed to creating a new one)</span>
            <span class="c1"># Note that we need to use the pk as defined by model_class, not</span>
            <span class="c1"># self.pk. These can be different fields because model inheritance</span>
            <span class="c1"># allows single model to have effectively multiple primary keys.</span>
            <span class="c1"># Refs #17615.</span>
            <span class="n">model_class_pk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">(</span><span class="n">model_class</span><span class="o">.</span><span class="n">_meta</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="ow">and</span> <span class="n">model_class_pk</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">exclude</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">model_class_pk</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">qs</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">unique_check</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">key</span> <span class="o">=</span> <span class="n">unique_check</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">key</span> <span class="o">=</span> <span class="n">NON_FIELD_ERRORS</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">unique_error_message</span><span class="p">(</span><span class="n">model_class</span><span class="p">,</span> <span class="n">unique_check</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_perform_date_checks</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date_checks</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">model_class</span><span class="p">,</span> <span class="n">lookup_type</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">unique_for</span> <span class="ow">in</span> <span class="n">date_checks</span><span class="p">:</span>
            <span class="n">lookup_kwargs</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="c1"># there&#39;s a ticket to add a date lookup, we can remove this special</span>
            <span class="c1"># case if that makes it&#39;s way in</span>
            <span class="n">date</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unique_for</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">date</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">lookup_type</span> <span class="o">==</span> <span class="s1">&#39;date&#39;</span><span class="p">:</span>
                <span class="n">lookup_kwargs</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">__day&#39;</span> <span class="o">%</span> <span class="n">unique_for</span><span class="p">]</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">day</span>
                <span class="n">lookup_kwargs</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">__month&#39;</span> <span class="o">%</span> <span class="n">unique_for</span><span class="p">]</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">month</span>
                <span class="n">lookup_kwargs</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">__year&#39;</span> <span class="o">%</span> <span class="n">unique_for</span><span class="p">]</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">year</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">lookup_kwargs</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">__</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">unique_for</span><span class="p">,</span> <span class="n">lookup_type</span><span class="p">)]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">lookup_type</span><span class="p">)</span>
            <span class="n">lookup_kwargs</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">)</span>

            <span class="n">qs</span> <span class="o">=</span> <span class="n">model_class</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">lookup_kwargs</span><span class="p">)</span>
            <span class="c1"># Exclude the current object from the query if we are editing an</span>
            <span class="c1"># instance (as opposed to creating a new one)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">exclude</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">pk</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">qs</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">date_error_message</span><span class="p">(</span><span class="n">lookup_type</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">unique_for</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">date_error_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lookup_type</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">unique_for</span><span class="p">):</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">field</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ValidationError</span><span class="p">(</span>
            <span class="n">message</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;unique_for_date&#39;</span><span class="p">],</span>
            <span class="n">code</span><span class="o">=</span><span class="s1">&#39;unique_for_date&#39;</span><span class="p">,</span>
            <span class="n">params</span><span class="o">=</span><span class="p">{</span>
                <span class="s1">&#39;model&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="p">,</span>
                <span class="s1">&#39;model_name&#39;</span><span class="p">:</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">capfirst</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)),</span>
                <span class="s1">&#39;lookup_type&#39;</span><span class="p">:</span> <span class="n">lookup_type</span><span class="p">,</span>
                <span class="s1">&#39;field&#39;</span><span class="p">:</span> <span class="n">field_name</span><span class="p">,</span>
                <span class="s1">&#39;field_label&#39;</span><span class="p">:</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">capfirst</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)),</span>
                <span class="s1">&#39;date_field&#39;</span><span class="p">:</span> <span class="n">unique_for</span><span class="p">,</span>
                <span class="s1">&#39;date_field_label&#39;</span><span class="p">:</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">capfirst</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">unique_for</span><span class="p">)</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)),</span>
            <span class="p">}</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">unique_error_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_class</span><span class="p">,</span> <span class="n">unique_check</span><span class="p">):</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="n">model_class</span><span class="o">.</span><span class="n">_meta</span>

        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;model&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="p">,</span>
            <span class="s1">&#39;model_class&#39;</span><span class="p">:</span> <span class="n">model_class</span><span class="p">,</span>
            <span class="s1">&#39;model_name&#39;</span><span class="p">:</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">capfirst</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)),</span>
            <span class="s1">&#39;unique_check&#39;</span><span class="p">:</span> <span class="n">unique_check</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="c1"># A unique field</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">unique_check</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">field</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">unique_check</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">params</span><span class="p">[</span><span class="s1">&#39;field_label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">capfirst</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="n">message</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;unique&#39;</span><span class="p">],</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;unique&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="c1"># unique_together</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">field_labels</span> <span class="o">=</span> <span class="p">[</span><span class="n">capfirst</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">unique_check</span><span class="p">]</span>
            <span class="n">params</span><span class="p">[</span><span class="s1">&#39;field_labels&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">get_text_list</span><span class="p">(</span><span class="n">field_labels</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;and&#39;</span><span class="p">)))</span>
            <span class="k">return</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="n">message</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%(model_name)s</span><span class="s2"> with this </span><span class="si">%(field_labels)s</span><span class="s2"> already exists.&quot;</span><span class="p">),</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;unique_together&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">,</span>
            <span class="p">)</span>

<div class="viewcode-block" id="Model.full_clean"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.full_clean">[docs]</a>    <span class="k">def</span> <span class="nf">full_clean</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">validate_unique</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calls clean_fields, clean, and validate_unique, on the model,</span>
<span class="sd">        and raises a ``ValidationError`` for any errors that occurred.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">exclude</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">exclude</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">exclude</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">clean_fields</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">errors</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">update_error_dict</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>

        <span class="c1"># Form.clean() is run even if other validation fails, so do the</span>
        <span class="c1"># same with Model.clean() for consistency.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">clean</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">errors</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">update_error_dict</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>

        <span class="c1"># Run unique checks, but only for fields that passed validation.</span>
        <span class="k">if</span> <span class="n">validate_unique</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">errors</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="n">NON_FIELD_ERRORS</span> <span class="ow">and</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                    <span class="n">exclude</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">validate_unique</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">errors</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">update_error_dict</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span></div>

<div class="viewcode-block" id="Model.clean_fields"><a class="viewcode-back" href="../../../../ref/models/instances.html#django.db.models.Model.clean_fields">[docs]</a>    <span class="k">def</span> <span class="nf">clean_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cleans all fields and raises a ValidationError containing a dict</span>
<span class="sd">        of all validation errors if any occur.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">exclude</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="c1"># Skip validation for empty fields with blank=True. The developer</span>
            <span class="c1"># is responsible for making sure they have a valid value.</span>
            <span class="n">raw_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">blank</span> <span class="ow">and</span> <span class="n">raw_value</span> <span class="ow">in</span> <span class="n">f</span><span class="o">.</span><span class="n">empty_values</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="n">raw_value</span><span class="p">,</span> <span class="bp">self</span><span class="p">))</span>
            <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">errors</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">error_list</span>

        <span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span></div>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_swappable</span><span class="p">())</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_model</span><span class="p">())</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_managers</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_fields</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_m2m_through_same_relationship</span><span class="p">())</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_long_column_names</span><span class="p">())</span>
            <span class="n">clash_errors</span> <span class="o">=</span> <span class="p">(</span>
                <span class="bp">cls</span><span class="o">.</span><span class="n">_check_id_field</span><span class="p">()</span> <span class="o">+</span>
                <span class="bp">cls</span><span class="o">.</span><span class="n">_check_field_name_clashes</span><span class="p">()</span> <span class="o">+</span>
                <span class="bp">cls</span><span class="o">.</span><span class="n">_check_model_name_db_lookup_clashes</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">clash_errors</span><span class="p">)</span>
            <span class="c1"># If there are field name clashes, hide consequent column name</span>
            <span class="c1"># clashes.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">clash_errors</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_column_name_clashes</span><span class="p">())</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_index_together</span><span class="p">())</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_unique_together</span><span class="p">())</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_ordering</span><span class="p">())</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_swappable</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Check if the swapped model exists. &quot;&quot;&quot;</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; is not of the form &#39;app_label.app_name&#39;.&quot;</span> <span class="o">%</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E001&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="k">except</span> <span class="ne">LookupError</span><span class="p">:</span>
                <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; references &#39;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&#39;, which has not been &quot;</span>
                        <span class="s2">&quot;installed, or is abstract.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                            <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span>
                        <span class="p">),</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E002&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_model</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">proxy</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span> <span class="ow">or</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;Proxy model &#39;</span><span class="si">%s</span><span class="s2">&#39; contains model fields.&quot;</span> <span class="o">%</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E017&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_managers</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Perform all manager checks. &quot;&quot;&quot;</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">manager</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managers</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">manager</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_fields</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Perform all field checks. &quot;&quot;&quot;</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">from_model</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_m2m_through_same_relationship</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Check if no relationship model is used by more than one m2m field.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">seen_intermediary_signatures</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">fields</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span>

        <span class="c1"># Skip when the target model wasn&#39;t found.</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">))</span>

        <span class="c1"># Skip when the relationship model wasn&#39;t found.</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="n">signature</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">signature</span> <span class="ow">in</span> <span class="n">seen_intermediary_signatures</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;The model has two many-to-many relations through &quot;</span>
                        <span class="s2">&quot;the intermediate model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">,</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E003&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">seen_intermediary_signatures</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">signature</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_id_field</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Check if `id` field is a primary key. &quot;&quot;&quot;</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;id&#39;</span> <span class="ow">and</span> <span class="n">f</span> <span class="o">!=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="p">)</span>
        <span class="c1"># fields is empty or consists of the invalid &quot;id&quot; field</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;id&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;id&#39; can only be used as a field name if the field also &quot;</span>
                    <span class="s2">&quot;sets &#39;primary_key=True&#39;.&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E004&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_field_name_clashes</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Ref #17673. &quot;&quot;&quot;</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">used_fields</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># name or attname -&gt; field</span>

        <span class="c1"># Check that multi-inheritance doesn&#39;t cause field name shadowing.</span>
        <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
                <span class="n">clash</span> <span class="o">=</span> <span class="n">used_fields</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="ow">or</span> <span class="n">used_fields</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">None</span>
                <span class="k">if</span> <span class="n">clash</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s2">&quot;The field &#39;</span><span class="si">%s</span><span class="s2">&#39; from parent model &quot;</span>
                            <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; clashes with the field &#39;</span><span class="si">%s</span><span class="s2">&#39; &quot;</span>
                            <span class="s2">&quot;from parent model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                                <span class="n">clash</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">clash</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span>
                                <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
                            <span class="p">),</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E005&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
                <span class="n">used_fields</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span>
                <span class="n">used_fields</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span>

        <span class="c1"># Check that fields defined in the model don&#39;t clash with fields from</span>
        <span class="c1"># parents, including auto-generated fields like multi-table inheritance</span>
        <span class="c1"># child accessors.</span>
        <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_fields</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">f</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">used_fields</span><span class="p">:</span>
                    <span class="n">used_fields</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span>

        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="n">clash</span> <span class="o">=</span> <span class="n">used_fields</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="ow">or</span> <span class="n">used_fields</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">None</span>
            <span class="c1"># Note that we may detect clash between user-defined non-unique</span>
            <span class="c1"># field &quot;id&quot; and automatically added unique field &quot;id&quot;, both</span>
            <span class="c1"># defined at the same model. This special case is considered in</span>
            <span class="c1"># _check_id_field and here we ignore it.</span>
            <span class="n">id_conflict</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;id&quot;</span> <span class="ow">and</span> <span class="n">clash</span> <span class="ow">and</span> <span class="n">clash</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;id&quot;</span> <span class="ow">and</span> <span class="n">clash</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="bp">cls</span>
            <span class="k">if</span> <span class="n">clash</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">id_conflict</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;The field &#39;</span><span class="si">%s</span><span class="s2">&#39; clashes with the field &#39;</span><span class="si">%s</span><span class="s2">&#39; &quot;</span>
                        <span class="s2">&quot;from model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                            <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">clash</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">clash</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
                        <span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="n">f</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E006&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="n">used_fields</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span>
            <span class="n">used_fields</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_column_name_clashes</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="c1"># Store a list of column names which have already been used by other fields.</span>
        <span class="n">used_column_names</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="n">_</span><span class="p">,</span> <span class="n">column_name</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get_attname_column</span><span class="p">()</span>

            <span class="c1"># Ensure the column name is not already in use.</span>
            <span class="k">if</span> <span class="n">column_name</span> <span class="ow">and</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="n">used_column_names</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;Field &#39;</span><span class="si">%s</span><span class="s2">&#39; has column name &#39;</span><span class="si">%s</span><span class="s2">&#39; that is used by &quot;</span>
                        <span class="s2">&quot;another field.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">column_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="s2">&quot;Specify a &#39;db_column&#39; for the field.&quot;</span><span class="p">,</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E007&#39;</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">used_column_names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">column_name</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_model_name_db_lookup_clashes</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">model_name</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="k">if</span> <span class="n">model_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">model_name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">):</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;The model name &#39;</span><span class="si">%s</span><span class="s2">&#39; cannot start or end with an underscore &quot;</span>
                    <span class="s2">&quot;as it collides with the query lookup syntax.&quot;</span> <span class="o">%</span> <span class="n">model_name</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E023&#39;</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="n">LOOKUP_SEP</span> <span class="ow">in</span> <span class="n">model_name</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;The model name &#39;</span><span class="si">%s</span><span class="s2">&#39; cannot contain double underscores as &quot;</span>
                    <span class="s2">&quot;it collides with the query lookup syntax.&quot;</span> <span class="o">%</span> <span class="n">model_name</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E024&#39;</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_index_together</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Check the value of &quot;index_together&quot; option. &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">index_together</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;index_together&#39; must be a list or tuple.&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E008&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>

        <span class="k">elif</span> <span class="nb">any</span><span class="p">(</span><span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">))</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">index_together</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;All &#39;index_together&#39; elements must be lists or tuples.&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E009&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">index_together</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_local_fields</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="s2">&quot;index_together&quot;</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_unique_together</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Check the value of &quot;unique_together&quot; option. &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;unique_together&#39; must be a list or tuple.&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E010&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>

        <span class="k">elif</span> <span class="nb">any</span><span class="p">(</span><span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">))</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;All &#39;unique_together&#39; elements must be lists or tuples.&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E011&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_check_local_fields</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="s2">&quot;unique_together&quot;</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_local_fields</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">option</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

        <span class="c1"># In order to avoid hitting the relation tree prematurely, we use our</span>
        <span class="c1"># own fields_map instead of using get_field()</span>
        <span class="n">forward_fields_map</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">field</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="p">}</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">field_name</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">field</span> <span class="o">=</span> <span class="n">forward_fields_map</span><span class="p">[</span><span class="n">field_name</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; refers to the non-existent field &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                            <span class="n">option</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span>
                        <span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E012&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyRel</span><span class="p">):</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; refers to a ManyToManyField &#39;</span><span class="si">%s</span><span class="s2">&#39;, but &quot;</span>
                            <span class="s2">&quot;ManyToManyFields are not permitted in &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                                <span class="n">option</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">option</span><span class="p">,</span>
                            <span class="p">),</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E013&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
                <span class="k">elif</span> <span class="n">field</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; refers to field &#39;</span><span class="si">%s</span><span class="s2">&#39; which is not local to model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span>
                            <span class="o">%</span> <span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="s2">&quot;This issue may be caused by multi-table inheritance.&quot;</span><span class="p">,</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E016&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_ordering</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Check &quot;ordering&quot; option -- is it a list of strings and do all fields</span>
<span class="sd">        exist? &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_ordering_clash</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;ordering&#39; and &#39;order_with_respect_to&#39; cannot be used together.&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E021&#39;</span><span class="p">,</span>
                <span class="p">),</span>
            <span class="p">]</span>

        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">ordering</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">ordering</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;ordering&#39; must be a tuple or list (even if you want to order by only one field).&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E014&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">ordering</span>

        <span class="c1"># Skip &#39;?&#39; fields.</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="n">f</span> <span class="o">!=</span> <span class="s1">&#39;?&#39;</span><span class="p">)</span>

        <span class="c1"># Convert &quot;-field&quot; to &quot;field&quot;.</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">((</span><span class="n">f</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;-&#39;</span><span class="p">)</span> <span class="k">else</span> <span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">)</span>

        <span class="c1"># Skip ordering in the format field1__field2 (FIXME: checking</span>
        <span class="c1"># this format would be nice, but it&#39;s a little fiddly).</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="n">LOOKUP_SEP</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">f</span><span class="p">)</span>

        <span class="c1"># Skip ordering on pk. This is always a valid order_by field</span>
        <span class="c1"># but is an alias and therefore won&#39;t be found by opts.get_field.</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">{</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="n">f</span> <span class="o">!=</span> <span class="s1">&#39;pk&#39;</span><span class="p">}</span>

        <span class="c1"># Check for invalid or non-existent fields in ordering.</span>
        <span class="n">invalid_fields</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Any field name that is not present in field_names does not exist.</span>
        <span class="c1"># Also, ordering by m2m fields is not allowed.</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">valid_fields</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span>
            <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">auto_created</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">concrete</span><span class="p">)</span> <span class="k">else</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">(),)</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">chain</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">related_objects</span><span class="p">)</span>
        <span class="p">))</span>

        <span class="n">invalid_fields</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">fields</span> <span class="o">-</span> <span class="n">valid_fields</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">invalid_field</span> <span class="ow">in</span> <span class="n">invalid_fields</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;ordering&#39; refers to the non-existent field &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="n">invalid_field</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E015&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_long_column_names</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check that any auto-generated column names are shorter than the limits</span>
<span class="sd">        for each database in which the model will be created.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">allowed_len</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">db_alias</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Find the minimum max allowed length among all specified db_aliases.</span>
        <span class="k">for</span> <span class="n">db</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">DATABASES</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="c1"># skip databases where the model won&#39;t be created</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="n">connection</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">db</span><span class="p">]</span>
            <span class="n">max_name_length</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">max_name_length</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">max_name_length</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">truncates_names</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">allowed_len</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">allowed_len</span> <span class="o">=</span> <span class="n">max_name_length</span>
                    <span class="n">db_alias</span> <span class="o">=</span> <span class="n">db</span>
                <span class="k">elif</span> <span class="n">max_name_length</span> <span class="o">&lt;</span> <span class="n">allowed_len</span><span class="p">:</span>
                    <span class="n">allowed_len</span> <span class="o">=</span> <span class="n">max_name_length</span>
                    <span class="n">db_alias</span> <span class="o">=</span> <span class="n">db</span>

        <span class="k">if</span> <span class="n">allowed_len</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">errors</span>

        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="n">_</span><span class="p">,</span> <span class="n">column_name</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get_attname_column</span><span class="p">()</span>

            <span class="c1"># Check if auto-generated name for the field is too long</span>
            <span class="c1"># for the database.</span>
            <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">db_column</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">column_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">column_name</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">allowed_len</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s1">&#39;Autogenerated column name too long for field &quot;</span><span class="si">%s</span><span class="s1">&quot;. &#39;</span>
                        <span class="s1">&#39;Maximum length is &quot;</span><span class="si">%s</span><span class="s1">&quot; for database &quot;</span><span class="si">%s</span><span class="s1">&quot;.&#39;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="n">column_name</span><span class="p">,</span> <span class="n">allowed_len</span><span class="p">,</span> <span class="n">db_alias</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="s2">&quot;Set the column name manually using &#39;db_column&#39;.&quot;</span><span class="p">,</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E018&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">:</span>
            <span class="c1"># Skip nonexistent models.</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="k">continue</span>

            <span class="c1"># Check if auto-generated name for the M2M field is too long</span>
            <span class="c1"># for the database.</span>
            <span class="k">for</span> <span class="n">m2m</span> <span class="ow">in</span> <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
                <span class="n">_</span><span class="p">,</span> <span class="n">rel_name</span> <span class="o">=</span> <span class="n">m2m</span><span class="o">.</span><span class="n">get_attname_column</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">m2m</span><span class="o">.</span><span class="n">db_column</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">rel_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">rel_name</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">allowed_len</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s1">&#39;Autogenerated column name too long for M2M field &#39;</span>
                            <span class="s1">&#39;&quot;</span><span class="si">%s</span><span class="s1">&quot;. Maximum length is &quot;</span><span class="si">%s</span><span class="s1">&quot; for database &quot;</span><span class="si">%s</span><span class="s1">&quot;.&#39;</span>
                            <span class="o">%</span> <span class="p">(</span><span class="n">rel_name</span><span class="p">,</span> <span class="n">allowed_len</span><span class="p">,</span> <span class="n">db_alias</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="p">(</span>
                                <span class="s2">&quot;Use &#39;through&#39; to create a separate model for &quot;</span>
                                <span class="s2">&quot;M2M and then set column_name using &#39;db_column&#39;.&quot;</span>
                            <span class="p">),</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">cls</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;models.E019&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

        <span class="k">return</span> <span class="n">errors</span></div>


<span class="c1">############################################</span>
<span class="c1"># HELPER FUNCTIONS (CURRIED MODEL METHODS) #</span>
<span class="c1">############################################</span>

<span class="c1"># ORDERING METHODS #########################</span>

<span class="k">def</span> <span class="nf">method_set_order</span><span class="p">(</span><span class="n">ordered_obj</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">id_list</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">using</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">using</span> <span class="o">=</span> <span class="n">DEFAULT_DB_ALIAS</span>
    <span class="n">order_wrt</span> <span class="o">=</span> <span class="n">ordered_obj</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span>
    <span class="n">filter_args</span> <span class="o">=</span> <span class="n">order_wrt</span><span class="o">.</span><span class="n">get_forward_related_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
    <span class="c1"># FIXME: It would be nice if there was an &quot;update many&quot; version of update</span>
    <span class="c1"># for situations like this.</span>
    <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">id_list</span><span class="p">):</span>
            <span class="n">ordered_obj</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">j</span><span class="p">,</span> <span class="o">**</span><span class="n">filter_args</span><span class="p">)</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">_order</span><span class="o">=</span><span class="n">i</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">method_get_order</span><span class="p">(</span><span class="n">ordered_obj</span><span class="p">,</span> <span class="bp">self</span><span class="p">):</span>
    <span class="n">order_wrt</span> <span class="o">=</span> <span class="n">ordered_obj</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span>
    <span class="n">filter_args</span> <span class="o">=</span> <span class="n">order_wrt</span><span class="o">.</span><span class="n">get_forward_related_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
    <span class="n">pk_name</span> <span class="o">=</span> <span class="n">ordered_obj</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span>
    <span class="k">return</span> <span class="n">ordered_obj</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">filter_args</span><span class="p">)</span><span class="o">.</span><span class="n">values_list</span><span class="p">(</span><span class="n">pk_name</span><span class="p">,</span> <span class="n">flat</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">make_foreign_order_accessors</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">related_model</span><span class="p">):</span>
    <span class="nb">setattr</span><span class="p">(</span>
        <span class="n">related_model</span><span class="p">,</span>
        <span class="s1">&#39;get_</span><span class="si">%s</span><span class="s1">_order&#39;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span>
        <span class="n">curry</span><span class="p">(</span><span class="n">method_get_order</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
    <span class="p">)</span>
    <span class="nb">setattr</span><span class="p">(</span>
        <span class="n">related_model</span><span class="p">,</span>
        <span class="s1">&#39;set_</span><span class="si">%s</span><span class="s1">_order&#39;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span>
        <span class="n">curry</span><span class="p">(</span><span class="n">method_set_order</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
    <span class="p">)</span>

<span class="c1">########</span>
<span class="c1"># MISC #</span>
<span class="c1">########</span>


<span class="k">def</span> <span class="nf">model_unpickle</span><span class="p">(</span><span class="n">model_id</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Used to unpickle Model subclasses with deferred fields.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model_id</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="o">*</span><span class="n">model_id</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Backwards compat - the model was cached directly in earlier versions.</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">model_id</span>
    <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>


<span class="n">model_unpickle</span><span class="o">.</span><span class="n">__safe_for_unpickle__</span> <span class="o">=</span> <span class="kc">True</span>


<span class="k">def</span> <span class="nf">unpickle_inner_exception</span><span class="p">(</span><span class="n">klass</span><span class="p">,</span> <span class="n">exception_name</span><span class="p">):</span>
    <span class="c1"># Get the exception class from the class it is attached to:</span>
    <span class="n">exception</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">klass</span><span class="p">,</span> <span class="n">exception_name</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">exception</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">exception</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>